package com.kcl.dfss;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;

public class ForgetPasswordActivity extends Activity implements OnClickListener{

	private LinearLayout forgetpwd_back;
	private EditText forget_phone,forget_code,forget_newpwd,forget_repeatpwd;
	private Button btn_forgetcode,btn_forget;
	
	
	private String phone,code,newpwd,repeatpwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_forget_password);
		
		forgetpwd_back = (LinearLayout) findViewById(R.id.forgetpwd_back);
		forget_phone = (EditText) findViewById(R.id.forget_phone);
		forget_code = (EditText) findViewById(R.id.forget_code);
		forget_newpwd = (EditText) findViewById(R.id.forget_newpwd);
		forget_repeatpwd = (EditText) findViewById(R.id.forget_repeatpwd);
		btn_forgetcode = (Button) findViewById(R.id.btn_forgetcode);
		btn_forget = (Button) findViewById(R.id.btn_forget);
		
		forgetpwd_back.setOnClickListener(this);
		btn_forgetcode.setOnClickListener(this);
		btn_forget.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.forgetpwd_back:
			finish();
			break;
		case R.id.btn_forgetcode:
			if(!AppUtils.stringFileter(forget_phone.getText().toString())){
				AppUtils.showDialog(ForgetPasswordActivity.this, "请输入正确的手机号");
			}else{
				if(!AppUtils.isNetworkAvailable(ForgetPasswordActivity.this)){
					Toast.makeText(ForgetPasswordActivity.this, "无网络连接", Toast.LENGTH_SHORT).show();
				}else{
					TimeCount timeCount = new TimeCount(60000, 1000);
					timeCount.start();
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							String result = ServerConnection.GetCode(forget_phone.getText().toString());
							if(result.equals("exception")){
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										Toast.makeText(ForgetPasswordActivity.this, "网络异常，请检查网络连接", Toast.LENGTH_SHORT).show();
									}
								});
							}
						}
					}).start();
				}
			}
			
			break;
		case R.id.btn_forget:
			phone = forget_phone.getText().toString();
			code = forget_code.getText().toString();
			newpwd = forget_newpwd.getText().toString();
			repeatpwd = forget_repeatpwd.getText().toString();
			if(phone.equals("")){
				showDialog(this, "请输入手机号");
			}else if(code.equals("")){
				showDialog(this, "请输入验证码");
			}else if(newpwd.equals("")){
				showDialog(this, "请输入新密码");
			}else if(repeatpwd.equals("")){
				showDialog(this, "请输入确认密码");
			}else if(!newpwd.equals(repeatpwd)){
				showDialog(this, "两次输入密码不同");
			}else{
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						String result = ServerConnection.RetrievePassword(newpwd, phone, code);
						JSONObject jsonObject;
						try {
							jsonObject = new JSONObject(result);
							final String getResult = jsonObject.getString("success");
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									if(getResult.equals("true")){
										Toast.makeText(ForgetPasswordActivity.this, "设置密码成功，请登录", Toast.LENGTH_SHORT).show();
//										startActivity(new Intent(ForgetPasswordActivity.this, LoginActivity.class));
										finish();
									}else{
										Toast.makeText(ForgetPasswordActivity.this, "设置密码失败", Toast.LENGTH_SHORT).show();
									}
								}
							});
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}).start();
			}
			break;
		}
	}
	
	class TimeCount extends CountDownTimer{
		
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}
		
		@Override
		public void onTick(long millisUntilFinished) {
			btn_forgetcode.setClickable(false);
			btn_forgetcode.setText(millisUntilFinished/1000+"秒");
		}
		
		@Override
		public void onFinish() {
			btn_forgetcode.setText("重新发送");
			btn_forgetcode.setClickable(true);
		}
		
	}
	
	public void showDialog(final Activity context,String message){
		Dialog dialog = new AlertDialog.Builder(context)
        .setTitle("系统提示") 
        .setMessage(message) 
        .setNeutralButton("确定", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                	dialog.dismiss();
                }
            })
        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        }).create();
        dialog.show(); 
	}

}
